package likou;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/11/09 09:01
 */
public class _488 {
    public static void main(String[] args) {
        _488 one = new _488();
        one.findMinStep("WWRRBBWW",
                "WRBRW");
    }

    public int findMinStep(String board, String hand) {
        char[] cs = board.toCharArray();
        char[] hs = hand.toCharArray();
        if (cs.length == 0) return hs.length;
        if (hs.length == 0) return -1;
        class Node {
            char c;
            Node next;
            Node pre;

            Node(char c) {
                this.c = c;
            }
        }
        Node head = new Node(cs[0]);
        Node temp = head;
        for (int i = 1; i < cs.length; i++) {
            temp.next = new Node(cs[i]);
            temp.next.pre = temp;
            temp = temp.next;
        }
        int i = 0;
        for (; i < hs.length && head != null; i++) {
            Node now = head;
            Node one = null;
            boolean p = false;
            while (now.next != null) {
                if (now.c == hs[i] && now.next.c == hs[i]) {
                    //直接消除这两个，然后退出循环
                    if (now.pre == null) {
                        head = now.next.next;
                        head.pre = null;
                    } else {
                        now.pre.next = now.next.next;
                    }
                    one = null;
                    p = true;
                    break;
                }
                if (now.c == hs[i] && one == null) {
                    one = now;
                }
                now = now.next;
            }
            if (!p) {
                if (one == null) {
                    one = now;
                }
                Node newNode = new Node(hs[i]);
                newNode.pre = one;
                newNode.next = one.next;
                one.next.pre = newNode;
                one.next = newNode;
            }
        }
        return head != null ? -1 : hs.length - i;
    }
}
